package com.o2opuzi.pub.component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import com.alibaba.fastjson.JSON;
import com.o2opuzi.pub.dao.ConfigDao;
import com.o2opuzi.pub.dao.MenuDao;
import com.o2opuzi.pub.model.Config;
import com.o2opuzi.pub.model.Menu;


/**
 * 系统启动时
 * @author Y.hj
 * @date 2016年6月17日
 */
@Component
public class AppComponent implements ServletContextAware,ApplicationContextAware,ApplicationListener<ContextRefreshedEvent>{
	
	private final Log log = LogFactory.getLog(getClass());
	
	private ServletContext servletContext;
	
	@Autowired
	private MenuDao menuDao;
	
	@Autowired
	private ConfigDao configDao;
	
	private ApplicationContext applicationContext;
	
	
	/**
	 * 加载菜单
	 * @auther Y.hj
	 */
	public void loadMenus(){
		log.info("正在加载系统菜单...");
		List<Menu> sysMenus = menuDao.findList();
		if(sysMenus == null){
			return;
		}
		Map<String, Menu> sysMenuMap = new HashMap<String,Menu>();
		for (Menu sysMenu : sysMenus) {
			sysMenuMap.put(sysMenu.getAction_url(),sysMenu);
		}
		setAttribute("system_menus", sysMenuMap);
		log.info("菜单加载完成...");
	}
	
	/**
	 * 加载配置文件
	 * @auther Y.hj
	 */
	public void loadConfig(){
		log.info("正在加载系统配置...");
		List<Config> configAll = configDao.findList();
		//System.out.println(JSON.toJSONString(configAll));
		for (Config config : configAll) {
			servletContext.setAttribute(config.getConfig_key(), JSON.parseObject(config.getConfig_value(), Map.class)); 
		}
		//System.out.println(JSON.toJSONString(servletContext.getAttribute("system_info")));
		log.info("系统配置加载完成...");
		
	}
	
	
	
	
	/**
	 * 系统启动时
	 * @auther Y.hj
	 */
	private void startup(){
		
		//加载配置文件
		loadConfig();
		
		//加载所有菜单
		loadMenus();
		
	}

	
	public void setAttribute(String name,Object value){
		servletContext.setAttribute(name, value);
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getAttribute(String name){
		return (T) servletContext.getAttribute(name);
	}
	
	public void removeAttribute(String name){
		servletContext.removeAttribute(name);
	}

	public List<Menu> getSysMenus() {
		return getAttribute("sysMenus");
	}
	
	public Map<String, Menu> getSysMenuMap(){
		return getAttribute("system_menus");
	}


	

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		this.startup();
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
		
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
			
		this.applicationContext = applicationContext;
		
	}

	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}
	
	public static void main(String[] args) {
	
	}
	
}
